package com.reny.ll.git.core

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding

abstract class RLazyFragment<VB : ViewDataBinding> : RBaseFragment<VB>() {

    var isFirstLoadPage = true // 是否第一次加载页面
    private var isViewCreated = false //是否创建好view
    protected var isVisibleToMe = false //是否可见

    open fun isLazyLoad():Boolean = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        isViewCreated = true
    }

    abstract fun lazyLoad()

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        isVisibleToMe = isVisibleToUser

        if(isVisibleToUser && !isFirstLoadPage){
            onVisibleAgain()
        }

        tempLoad()
    }

    override fun onResume() {
        super.onResume()

        tempLoad()
    }

    //当前fragment再次可见时执行  第一次可见不执行
    open fun onVisibleAgain(){

    }

    private fun tempLoad(){
        if(isFirstLoadPage && isLazyLoad() && isVisibleToMe && isViewCreated){
            lazyLoad()
            isFirstLoadPage = false
        }
    }

}